home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacGS 2.5.2ß3 / (genarch) / genarch.c next >
Encoding:
C/C++ Source or Header  |  1993-04-17  |  2.2 KB  |  64 lines  |  [TEXT/R*ch]

  1. /* genarch.c */
  2. /* Generate a header file (arch.h) with parameters */
  3. /* reflecting the machine architecture and compiler characteristics. */
  4.  
  5. #include <stdio.h>
  6.  
  7. /* We should write the result on stdout, but the Turbo C 'make' */
  8. /* can't handle output redirection (sigh). */
  9.  
  10. main(argc, argv)
  11.     int argc;
  12.     char *argv[];
  13. {    
  14.     char *fname = argv[1];
  15.     long one = 1;
  16.     struct { char c; short s; } ss;
  17.     struct { char c; long l; } sl;
  18.     struct { char c; float f; } sf;
  19.     struct { char c; double d; } sd;
  20.     long lm1 = -1;
  21.     long lr1 = lm1 >> 1, lr2 = lm1 >> 2;
  22.     unsigned long um1 = ~(unsigned long)0;
  23.     int im1 = -1;
  24.     int ir1 = im1 >> 1, ir2 = im1 >> 2;
  25.     int ars;
  26.     int lwidth = sizeof(long) * 8;
  27.     float f0 = 0.0, f1 = 1.0, fm1 = -1.0;
  28.     FILE *f = fopen(fname, "w");
  29.     if ( f == NULL )
  30.        {    fprintf(stderr, "genarch.c: can't open %s for writing\n", fname);
  31.         exit(1);
  32.        }
  33.     fprintf(f, "/* Parameters derived from machine and compiler architecture */\n\n");
  34.     fprintf(f, "#define arch_is_big_endian %d\n", 1 - *(char *)&one);
  35.     fprintf(f, "#define arch_align_short_mod %ld\n",
  36.         (char *)&ss.s - &ss.c);
  37.     fprintf(f, "#define arch_align_long_mod %ld\n",
  38.         (char *)&sl.l - &sl.c);
  39.     fprintf(f, "#define arch_align_float_mod %ld\n",
  40.         (char *)&sf.f - &sf.c);
  41.     fprintf(f, "#define arch_align_double_mod %ld\n",
  42.         (char *)&sd.d - &sd.c);
  43.     fprintf(f, "#define arch_sizeof_short %d\n", (short) sizeof(short));
  44.     fprintf(f, "#define arch_sizeof_int %d\n", (short) sizeof(int));
  45.     fprintf(f, "#define arch_sizeof_long %d\n", (short) sizeof(long));
  46.     fprintf(f, "#define arch_floats_are_IEEE %d\n",
  47.         (*(long *)&f0 == 0 && *(long *)&f1 == 0x3f800000L &&
  48.          *(long *)&fm1 == 0xbf800000L ? 1 : 0));
  49.     /* There are three cases for arithmetic right shift: */
  50.     /* always correct, correct except for right-shifting a long by 1 */
  51.     /* (a bug in some versions of the Turbo C compiler), and */
  52.     /* never correct. */
  53.     ars = (lr2 != -1 || ir1 != -1 || ir2 != -1 ? 0 :
  54.         lr1 != -1 ? 1 :        /* Turbo C problem */
  55.         2);
  56.     fprintf(f, "#define arch_arith_rshift %d\n", ars);
  57.     /* Some machines can't handle a variable shift by */
  58.     /* the full width of a long. */
  59.     fprintf(f, "#define arch_can_shift_full_long %d\n",
  60.         um1 >> lwidth == 0);
  61.     fclose(f);
  62.     return 0;
  63. }
  64.